package leetCode;

import java.util.*;

/**
 * 移除字母异位词后的结果数组
 *
 * @author 司松坡
 * @createTime 2023年04月05日
 */
public class LeetCode2273 {
    public List<String> removeAnagrams(String[] words) {
        List<String> res = new ArrayList<>();
        if (words.length == 1){
            return Arrays.asList(words);
        }
        res.add(words[0]);
        for (int i = 1; i < words.length; i++) {
            if (!isTrue(words[i],words[i-1])){
                res.add(words[i]);
            }
        }
        return res;
    }

    public boolean isTrue(String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        Map<Character, Integer> count = count(s1);
        Map<Character, Integer> count1 = count(s2);
        for (Character key : count1.keySet()) {
            if (!count.get(key).equals(count1.get(key))){
                return false;
            }
        }
        return true;
    }

    public Map<Character, Integer> count(String s) {
        Map<Character, Integer> res = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            Integer integer = res.get(c);
            if (integer == null) {
                res.put(c, 1);
            } else {
                res.replace(c, integer + 1);
            }
        }
        return res;
    }

}
